import pygame

class Room:
	def __init__(self):
		self.platformList_lu=[]
		self.platformList_ru=[]#self.platformList_lu[:]
		self.platformList_rd=[]#self.platformList_lu[:]
		self.platformList_ld=[]#self.platformList_lu[:]
		self.protagonists=[]
		self.containers=[]
		self.corpsesList=[]
		self.projectilesList=[]
		self.junkList=[]
		self.collectablesList=[]
	def draw(self,screen,camera):
		screen.fill((0,0,0))
		for coll in self.collectablesList:
			c,i=coll.drawingInformation()
			c=camera.transformCoord(c)
			screen.blit(i,c)
		for container in self.containers:
			c,i=container.drawingInformation()
			c=camera.transformCoord(c)
			screen.blit(i,c)
		for p in self.junkList:
			c,i=p.drawingInformation()
			c=camera.transformCoord(c)
			screen.blit(i,c)
		for p in self.platformList_lu:
			c,i=p.drawingInformation()
			if not c is None and not i is None:
				c=camera.transformCoord(c)
				screen.blit(i,c)
		for p in self.projectilesList:
			c,i=p.drawingInformation()
			c=camera.transformCoord(c)
			screen.blit(i,c)
		for hero1 in self.protagonists:
			c,i=hero1.drawingInformation()
			c=camera.transformCoord(c)
			screen.blit(i,c)
		#visualy check if body link is correct
		for corpse in self.corpsesList:
			for link in corpse.links: 
				c1=link.get_coord_hole1()
				c2=link.get_coord_hole2()
				print "b parts"
				print [link.part1.x,link.part1.y],c1,link.part1.cur_angle
				print [link.part2.x,link.part2.y],c2,link.part2.cur_angle
				c1=camera.transformCoord(c1)
				c2=camera.transformCoord(c2)
				pygame.draw.line(screen,(0,200,0),c1,c2)
	def collisionDetection(self,objecto,list):
		i=0
		while objecto.mFreedom() and i<len(list):
			list[i].objectLanding(objecto)
			i=i+1
	def process(self):
		deceased=[]
		if len(self.projectilesList)>0:
			allT_lu=self.platformList_lu[:]
			allT_lu.extend(self.protagonists)
			allT_lu.extend(self.containers)
			allT_lu.sort(key=lambda p: (p.leftSide(),-p.upperSide()))
			allT_ru=self.platformList_ru[:]
			allT_ru.extend(self.protagonists)
			allT_ru.extend(self.containers)
			allT_ru.sort(key=lambda p: (-p.rightSide(),-p.upperSide()))
			allT_ld=self.platformList_ld[:]
			allT_ld.extend(self.protagonists)
			allT_ld.extend(self.containers)
			allT_ld.sort(key=lambda p: (p.leftSide(),p.lowerSide()))
			allT_rd=self.platformList_rd[:]
			allT_rd.extend(self.protagonists)
			allT_rd.extend(self.containers)
			allT_rd.sort(key=lambda p: (-p.rightSide(),p.lowerSide()))
		projectilesList=self.projectilesList[:]
		for p in projectilesList:
			p.process()
			if p.x>p.prev_x and p.y<p.prev_y:
				self.collisionDetection(p,allT_lu)
			elif p.y<p.prev_y:
				self.collisionDetection(p,allT_ru)
			elif p.x>p.prev_x:
				self.collisionDetection(p,allT_ld)
			else:
				self.collisionDetection(p,allT_rd)
		for j in self.junkList:
			j.process()
			for p in self.platformList_lu:
				p.objectLanding(j)
		#self.plat_coll_lu=self.platformList_lu[:]
		#self.plat_coll_lu.extend(self.collectablesList)
		protagonists=self.protagonists[:]
		for hero1 in protagonists:
			print "hero <s>"
			hero1.process()
			self.collisionDetection(hero1,self.platformList_lu)
			for coll in self.collectablesList:
				coll.objectLanding(hero1)
			'''if hero1.x>=hero1.prev_x and hero1.y<=hero1.prev_y:
				self.collisionDetection(hero1,self.platformList_lu)
			elif hero1.y<=hero1.prev_y:
				self.collisionDetection(hero1,self.platformList_ru)
			elif hero1.x>hero1.prev_x:
				self.collisionDetection(hero1,self.platformList_ld)
			else:
				self.collisionDetection(hero1,self.platformList_rd)'''
			if hero1.cur_health<=0:
				hero1.die()
			if hero1.dead:
				deceased.append(hero1)
			print "hero </s>"
		for container in self.containers:
			container.process()
			for p in self.platformList_lu:
				p.objectLanding(container)
			if container.dead:
				deceased.append(container)
		for p in self.platformList_lu:
			p.process()
		self.solveDeceased( deceased)
		for corpse in self.corpsesList:
			corpse.process()	
	def solveDeceased(self,deceased):
		#import BodyPart
		#import RagdollCorpse
		for hero1 in deceased:
			hero1.deathConfirmation()
			#self.containers.append(hero1)
	def addAnotherPlatform(self,another):
		self.platformList_lu.append(another)
		self.platformList_ld.append(another)
		self.platformList_ru.append(another)
		self.platformList_rd.append(another)
		self.platformList_lu.sort(key=lambda p: (p.leftSide(),-p.upperSide()))
		self.platformList_ld.sort(key=lambda p: (p.leftSide(),p.lowerSide()))
		self.platformList_ru.sort(key=lambda p: (-p.rightSide(),-p.upperSide()))
		self.platformList_rd.sort(key=lambda p: (-p.rightSide(),p.lowerSide()))
	def addAnotherContainer(self,another):
		another.cur_room=self
		self.containers.append(another)
	def platforms_from_xml(self,doc):
		import Coping
		import Hurdle
		import Ledge
		import Wippe
		import Wall
		import TrapDoor
		import ConveyorBelt
		import Canopy
		import SpikyPlate
		import Aerostat
		import MechanicalPress
		import PowerSaw
		import ShockBeamBarrier
		import Pole
		import Plank
		
		for node in doc.getElementsByTagName("Wippe"): 
			print "Wippe"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=Wippe.Wippe(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Coping"): 
			print "Coping"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			w=node.getAttribute("width")
			if len(w)>0:
				width=float(w)
			else:
				width=250
			print x,y
			p=Coping.Coping(pos=(x,y),width=width)
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Plank"): 
			print "Plank"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			w=node.getAttribute("width")
			if len(w)>0:
				width=float(w)
			else:
				width=250
			print x,y
			p=Plank.Plank(pos=(x,y),width=width)
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Hurdle"):
			print "Hurdle"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			h=node.getAttribute("height")
			if len(h)>0:
				height=float(h)
			else:
				height=250
			print x,y
			p=Hurdle.Hurdle(pos=(x,y),height=height)
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Ledge"):
			print "Ledge"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=Ledge.Ledge(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("TrapDoor"):
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=TrapDoor.TrapDoor(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Wall"):
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			h=node.getAttribute("height")
			if len(h)>0:
				height=float(h)
			else:
				height=250
			print x,y
			print x,y
			p=Wall.Wall(pos=(x,y),height=height)
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("ConveyorBelt"):
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			rface=float(node.getAttribute("rface") )
			rface=bool(rface)
			print x,y
			p=ConveyorBelt.ConveyorBelt(pos=(x,y),rface=rface)
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Canopy"):
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=Canopy.Canopy(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("SpikyPlate"):
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=SpikyPlate.SpikyPlate(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Aerostat"):
			print "Aerostat"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=Aerostat.Aerostat(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("MechanicalPress"):
			print "MechanicalPress"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=MechanicalPress.MechanicalPress(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("ShockBeamBarrier"):
			print "ShockBeamBarrier"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=ShockBeamBarrier.ShockBeamBarrier(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("PowerSaw"):
			print "PowerSaw"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=PowerSaw.PowerSaw(pos=(x,y))
			self.addAnotherPlatform(p)
		for node in doc.getElementsByTagName("Pole"):
			print "Pole"
			x=float(node.getAttribute("x") )
			y=float(node.getAttribute("y") )
			print x,y
			p=Pole.Pole(pos=(x,y))
			self.addAnotherPlatform(p)
		#for i in xrange(0,len(self.platformList_lu)):
		#	print 'ld',self.platformList_lu[i].leftSide(),self.platformList_lu[i].lowerSide()
	def containers_from_xml(self,doc):
		import Coffer
		import MonkeyJar
		
		for node in doc.getElementsByTagName("Coffer"): 
			print "Coffer"
			x=float(node.getAttribute("x"))
			y=float(node.getAttribute("y"))
			print x,y
			cont=Coffer.Coffer(x=x,y=y)
			self.addAnotherContainer(cont)
		for node in doc.getElementsByTagName("MonkeyJar"): 
			print "MonkeyJar"
			x=float(node.getAttribute("x"))
			y=float(node.getAttribute("y"))
			print x,y
			cont=MonkeyJar.MonkeyJar(x=x,y=y)
			self.addAnotherContainer(cont)
	def construct_from_xml(self,file_name):
		import xml.dom.minidom 
		from xml.dom.minidom import Node
		
		doc = xml.dom.minidom.parse(file_name) 
		for platformList in doc.getElementsByTagName("platformList"):
			self.platforms_from_xml(platformList)
		for containers in doc.getElementsByTagName("containers"):
			self.containers_from_xml(containers)
	def addAnotherCharacter(self,another):
		self.protagonists.append(another)
		another.cur_room=self
	def removePlatform(self,platform):
		self.platformList_lu.remove(platform)
		self.platformList_ld.remove(platform)
		self.platformList_ru.remove(platform)
		self.platformList_rd.remove(platform)
	def addAnotherCollectable(self,collectable):
		self.collectablesList.append(collectable)
		collectable.cur_room=self
def getTestRoom():
	test=Room()
	test.construct_from_xml("testRoom.xml")
	import Rope
	#test.junkList.extend(Rope.testKnots)
	#test.platformList.extend(Rope.testSegments)
	import Character
	test.addAnotherCharacter(Character.Character(x=300,y=500))
	import Collectable
	test.addAnotherCollectable(Collectable.Collectable(x=120))
	test.addAnotherCollectable(Collectable.Collectable(x=170))
	test.addAnotherCollectable(Collectable.Collectable(x=220))
	test.addAnotherCollectable(Collectable.Collectable(x=520))
	test.addAnotherCollectable(Collectable.Collectable(x=720))
	return test